package com.css.security.core.config;

import com.css.common.security.constant.SecurityConstants;
import com.css.common.security.properties.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

/**
 * FormAuthenticationConfig
 * 表单登陆配置
 *
 * @author hanyx
 * @date 2019/04/12
 */
@Component
public class FormAuthenticationConfig {

    @Autowired
    private AuthenticationSuccessHandler authenticationSuccessHandler;

    @Autowired
    private AuthenticationFailureHandler authenticationFailureHandler;

    @Autowired
    @Qualifier("cssAccessDeniedHandler")
    private AccessDeniedHandler accessDeniedHandler;

    public void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                // 需要身份认证的请求, 线条转到 /authentication/require 请求中
                .loginPage(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL)
//                .loginPage(securityProperties.getBrowser().getSignInPage())
                // 用户密码登录请求路径
                .loginProcessingUrl(SecurityConstants.DEFAULT_SIGN_IN_PROCESSING_URL_FORM)
                .successHandler(authenticationSuccessHandler)
                .failureHandler(authenticationFailureHandler)
                .and()
        .exceptionHandling().accessDeniedHandler(accessDeniedHandler);

    }
}
